package Viper

import (
	"fmt"
	global "gPublic/Global"
	"gPublic/config"

	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

var ServerConfig = config.ServerConfig{}

func ViperTest() {
	v := viper.New()
	v.SetConfigFile("config-develop.yml")

	if err := v.ReadInConfig(); err != nil {

		panic(err)
	}

	ServerInfo := &ServerConfig

	err := v.Unmarshal(&ServerInfo)
	if err != nil {
		panic(err)
	}

	v.WatchConfig()
	v.OnConfigChange(func(in fsnotify.Event) {
		fmt.Println("config file changed... ")

		_ = v.ReadInConfig()
		_ = v.Unmarshal(&ServerInfo)
	})

	global.Config = ServerInfo
}
